package com.sangmo.fts.process.receivable.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.plugin.dataview.crud.service.DelegateViewService;
import com.sangmo.boot.framework.plugin.dataview.crud.service.ViewService;
import com.sangmo.fts.process.receivable.model.RpfPledgeContract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RpfPledgeContractService extends DelegateViewService<RpfPledgeContract> {

    @Autowired
    private RpfPledgeContractEntityService rpfPledgeContractEntityService;

    @Override
    protected ViewService<RpfPledgeContract> getEntityService() {
        return rpfPledgeContractEntityService;
    }

    public void pledgorConfirm(Long id) {
        RpfPledgeContract contract = this.get(id);
        if (null == contract.getPledgorConfirmDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.pledgorConfirm.permit.disabled");
        }
        if (contract.isPledgorConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.pledgorConfirm.state.wrong");
        }
        rpfPledgeContractEntityService.pledgorConfirm(contract);
    }

    public void pledgeeConfirm(Long id) {
        RpfPledgeContract contract = this.get(id);
        if (null == contract.getPledgeeConfirmDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.pledgeeConfirm.permit.disabled");
        }
        if (contract.isPledgeeConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.pledgeeConfirm.state.wrong");
        }
        rpfPledgeContractEntityService.pledgeeConfirm(contract);
    }

    public void register(Long id) {
        RpfPledgeContract contract = this.get(id);
        if (contract.isRegistered()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.register.duplicate");
        }
        contract.setRegistered(true);
        this.save(contract);
    }

    public void notify(Long id) {
        RpfPledgeContract contract = this.get(id);
        if (!contract.isRegistered()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.notify.permit.disabled");
        }
        contract.setNotified(true);
        this.save(contract);
    }

    public void buyerConfirm(Long id) {
        RpfPledgeContract contract = this.get(id);
        if (null == contract.getBuyerConfirmDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.buyerConfirm.permit.disabled");
        }
        if (contract.isBuyerConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.buyerConfirm.state.wrong");
        }
        rpfPledgeContractEntityService.buyerConfirm(contract);
    }
}
